﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace CAUNetLogin.NetUtil.ReturnMessage
{

    /// <summary>
    /// 该类用来通过使用网页返回的信息（已经提取了信息），给用户提示详细的登录信息
    /// </summary>
    public class FailedMessage
    {
        public int msgID = 0;//返回的代码号
        public string msg = "";//消息具体描述
        public bool isEror = false;
        public string lastErrorMessage = "this is the default error message";
        public double balance = 0;//账户余额
        public long time;//本月已使用时间（Minute）
        public double flowUsed = 0.0;//本月已使用流量MB

        private string msga = "";
        private string xip;
        private string mac;
        private string v6ip;
        private long flow;//本月已使用流量KB
        private int fsele;
        private long fee;
        private int xsele;

        private string ut = "";//已使用分钟
        private string uf = "";//已使用流量
        private string um = "";//余额

        /// <summary>
        /// 获得网页中的错误信息代码
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public bool ParseMsg(string html)
        {
            string tempMain = Regex.Replace(html.Substring(120, 500), "\r\n", "");//去除回车换行
            try
            {
                string pattern = @".*Msg=(?<Msg>\d*);time='(?<time>\d*)\s*';flow='(?<flow>\d*)\s*';fsele=(?<fsele>\d*);fee='(?<fee>\d*)\s*';xsele=(?<xsele>\d*);xip='(?<xip>.*)';mac='(?<mac>.*)';.*msga='(?<msga>.*)';.*v6ip='(?<v6ip>.*)';";//
                Regex r = new Regex(pattern);
                if (r.IsMatch(tempMain))
                {
                    Match match = r.Match(tempMain);
                    Int32.TryParse(match.Groups["Msg"].ToString(), out msgID);//返回的代码号
                    Int64.TryParse(match.Groups["time"].ToString(), out time);
                    Int64.TryParse(match.Groups["flow"].ToString(), out flow);
                    Int32.TryParse(match.Groups["fsele"].ToString(), out fsele);
                    Int64.TryParse(match.Groups["fee"].ToString(), out fee);
                    Int32.TryParse(match.Groups["xsele"].ToString(), out xsele);
                    xip = match.Groups["xip"].ToString();//外网IP
                    mac = match.Groups["mac"].ToString();
                    msga = match.Groups["msga"].ToString();
                    v6ip = match.Groups["v6ip"].ToString();
                    getMessageDetails();
                }

            }
            catch (Exception ex)
            {
                lastErrorMessage = ex.Message;
                return false;
            }
            return true;
        }


        /// <summary>
        /// 通过解析错误代码得到具体的错误信息
        /// </summary>
        /// <returns></returns>
        private string getMessageDetails()
        {

            if (v6ip != "" && xip == "0.0.0.0")
                xip = v6ip;

            balance = (fee - fee % 100) / 10000.0;
            flowUsed = flow / 1024.0;

            ut = "本月已使用时间:" + time + "分钟";
            uf = "本月已使用流量:" + flowUsed+ " MByte";
            if (fsele == 1)
                um = "余额 : " + balance+ " 元";
            else
                um = "无法获取余额信息";
         
            switch (msgID)
            {
                case 0:
                case 1:
                    if (msga != "")
                    {
                        switch (msga)
                        {
                            case "error0":
                                msg = "本IP不允许Web方式登录";
                                break;
                            case "error1":
                                msg = "本用户不允许Web方式登录";
                                break;
                            case "error2":
                                msg = "本用户不允许修改密码";
                                break;
                            default:
                                if (msga == "[01], 本帐号只能在指定VLANID(55)使用")
                                {
                                    msg = "未开户，无法使用网关";
                                }
                                else
                                {
                                    msg = msga;
                                }
                                break;
                        }
                    }
                    else
                    { msg = "用户名或密码不对，请重新输入"; }
                    break;
                case 2:
                    msg = "该账号正在IP为：" + xip + "的机器上使用，请断开它的连接并重新输入用户名和密码登录本机";
                    break;
                case 3:
                    msg = "本帐号只能在指定地址使用：" + xip;
                    break;
                case 4:
                    msg = "本月超出使用限额或余额不足";
                    break;
                case 5:
                    msg = "本用户暂停使用";
                    break;
                case 6:
                    msg = "System buffer full";
                    break;
                case 7:
                    msg = ut + uf + um;
                    break;
                case 8:
                    msg = "本用户正在使用,不能修改";
                    break;
                case 9:
                    msg = "新密码与确认新密码不匹配,不能修改";
                    break;
                case 10:
                    msg = "密码修改成功";
                    break;
                case 11:
                    msg = "本用户只能在指定地址使用 :" + mac;
                    break;
                //case 12 and 13 are not aviable
                case 14:
                    msg = "注销成功" + ut + uf + um;
                    break;
                case 15:
                    msg = "登录成功" + ut + uf + um; ;
                    break;
                default:
                    msg = "这是默认消息";
                    break;
            }
            return msg;
        }
    }
}
